package org.goj.common.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * @author CK
 * @date 2021/4/5 15:07
 */
public class BingImageUtil {

    /**
     * 获取必应每日一图
     *
     * @return String
     * @author CK
     * @date 2021/4/5 15:28
     */
    public static String getUrl() {
        try {
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> responseEntity = restTemplate
                    .getForEntity("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1", String.class);
            JSONArray jsonArray = JSONUtil.parseObj(responseEntity.getBody()).getJSONArray("images");
            return "https://cn.bing.com" + JSONUtil.parseObj(jsonArray.get(0).toString()).get("url").toString();
        } catch (Exception e) {
            return "https://wpimg.wallstcn.com/e7d23d71-cf19-4b90-a1cc-f56af8c0903d.png";
        }
    }

}
